package com.ruicar.afs.cloud.manage.workflow.response;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.ruicar.afs.cloud.common.core.enums.AfsEnumUtil;
import com.ruicar.afs.cloud.manage.workflow.enums.WorkflowInvokeStatusEnum;
import lombok.Data;

import java.io.Serializable;

/**
 * @Description 返回对象需要根据对应工作流口文档规范定义
 * @Author Peng_Jinbo
 * @Date 2020/06/18 20:22
 */
@Data
public abstract class WorkflowResponse implements Serializable {

    @JSONField(serialize = false)
    private String status;
    @JSONField(serialize = false)
    private String message;

    public WorkflowResponse success(String msg){
        this.status = AfsEnumUtil.key(WorkflowInvokeStatusEnum.SUCCESS);
        this.message = msg;
        return this;
    }

    public WorkflowResponse failed(String msg){
        this.status = AfsEnumUtil.key(WorkflowInvokeStatusEnum.FAILED);
        this.message = msg;
        return this;
    }

    public WorkflowResponse message(String msg){
        this.message = msg;
        return this;
    }

    public boolean successful(){
        return AfsEnumUtil.key(WorkflowInvokeStatusEnum.FAILED).equals(this.status);
    }

    public JSON toJsonObj(){
        return JSONObject.parseObject(JSON.toJSONString(this));
    }

    public static final WorkflowResponse empty() {
        return new EmptyResponse().success(AfsEnumUtil.desc(WorkflowInvokeStatusEnum.SUCCESS));
    }
}
